package m.system.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import m.system.RuntimeData;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

public class ImageUtil {

	/**
	 * 保存缩略图
	 *  file 源文件
	 *  url_path 根目录开始的url
	 *  IOException
	 */
	public static void saveThumFile(File file,String url_path) throws IOException{
		saveThumFile(file, url_path, 300, 300);
	}
	public static void saveThumFile(File file,String url_path,int width) throws IOException{
		saveThumFile(file, url_path, width, width);
	}
	public static void saveThumFile(File file,String url_path,int width,int height) throws IOException{
		File f=FileUtil.getFile(RuntimeData.getPublicWeb() + url_path);
		BufferedImage bi=ImageIO.read(file);
		double n= Math.min(bi.getHeight() / (double) height, bi.getWidth() / (double) width);
		int w=new Double(Math.floor(bi.getWidth()/n)).intValue(),h=new Double(Math.floor(bi.getHeight()/n)).intValue();
		if(w<width) w=width;
		if(h<height) h=height;
		int y=(h-w)/2-(height-width)/2,x=(w-h)/2-(width-height)/2;
		if(x<0) x=0;
		if(y<0) y=0;
		Image img = bi.getScaledInstance(w , h, Image.SCALE_DEFAULT);
		//创建一个新的缓存图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获取画笔
        Graphics2D graphics = image.createGraphics();
        //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
        graphics.drawImage(img, -x, -y,null);
        //一定要释放资源
        graphics.dispose();
        //获取到文件的后缀名
        String fileName = f.getName();
        String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
        //使用ImageIO的write方法进行输出
        ImageIO.write(image,formatName,f);
	}
	/**
	 * 保存显示图
	 *  file 源文件
	 *  url_path 根目录开始的url
	 *  IOException
	 */
	public static void saveImgFile(File file,String url_path) throws IOException{
		saveFile(file, url_path, 1920);
	}
	/**
	 * 保存分类文件
	 *  file 源文件
	 *  url_path 根目录开始的url
	 *  IOException
	 */
	public static void saveFile(File file,String url_path,int maxWidth) throws IOException{
		BufferedImage bi=ImageIO.read(file);
		double n=(Math.max(bi.getWidth(), bi.getHeight()))/ (double) maxWidth;
		saveThumFile(file, url_path,NumberUtil.toInt(bi.getWidth()/n), NumberUtil.toInt(bi.getHeight()/n));
	}
	/**
	 * 保存字符到二维码文件
	 *  text
	 *  path
	 *  width
	 *  WriterException
	 *  IOException
	 */
	public static void saveQRCode(String text,String url_path,int width) throws WriterException, IOException{
		saveQRCode4Path(text, RuntimeData.getPublicWeb() + url_path, width);
	}
	public static void saveQRCode4Path(String text,String path,int width) throws WriterException, IOException{
		ImageIO.write(toQRCode(text, width), "png",FileUtil.getFile(path));
	}
	public static BufferedImage toQRCode(String text,int width) throws WriterException{
		Hashtable<EncodeHintType, Object> hints= new Hashtable<>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE,width,width,hints); 
		BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < width; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		return image;
	}
	public static void main(String[] a){
		try {
			saveQRCode4Path("http://xiaomao.fulicat.cn","d:/1.jpg",300);
		} catch (WriterException | IOException e) {
			e.printStackTrace();
		}
	}
}
